#include "../include/ViewGroup.h"
#include "../base.h"

#define LOG_TAG "ViewGroup"

static jclass getClass(JNIEnv* env,const char * className)
{
	return (*env)->FindClass(env,className);
}

static jclass getViewGroupClass(JNIEnv* env)
{
	return getClass(env,"android/view/ViewGroup");
}

void deleteViewGroup(ViewGroup thiz)
{
	if(thiz==NULL)
	{
		LOGE("deleteViewGroup thiz==NULL");
		return ;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	(*env)->DeleteGlobalRef(env,thiz);
}

//添加View
void ViewGroup_addView(ViewGroup thiz,View child)
{
	if(thiz==NULL)
	{
		LOGE("ViewGroup_addView thiz==NULL");
		return ;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getViewGroupClass(env);
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID(env,clazz,"addView","(Landroid/view/View;)V");
	(*env)->DeleteLocalRef(env,clazz);
	(*env)->CallVoidMethod(env,thiz,methoID,child);
}

//添加View
void ViewGroup_addViewAt(ViewGroup thiz,View child,int index)
{
	if(thiz==NULL)
	{
		LOGE("ViewGroup_addViewAt thiz==NULL");
		return ;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getViewGroupClass(env);
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID(env,clazz,"addView","(Landroid/view/View;I)V");
	(*env)->DeleteLocalRef(env,clazz);
	(*env)->CallVoidMethod(env,thiz,methoID,child,index);
}

//添加View
void ViewGroup_addViewWH(ViewGroup thiz,View child,int width,int height)
{
	if(thiz==NULL)
	{
		LOGE("ViewGroup_addViewWH thiz==NULL");
		return ;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getViewGroupClass(env);
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID(env,clazz,"addView","(Landroid/view/View;II)V");
	(*env)->DeleteLocalRef(env,clazz);
	(*env)->CallVoidMethod(env,thiz,methoID,child,width,height);
}

//添加View
void ViewGroup_addViewParams(ViewGroup thiz,View child,LayoutParams params)
{
	if(thiz==NULL)
	{
		LOGE("ViewGroup_addViewParams thiz==NULL");
		return ;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getViewGroupClass(env);
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID(env,clazz,"addView","(Landroid/view/View;Landroid/view/ViewGroup$LayoutParams;)V");
	(*env)->DeleteLocalRef(env,clazz);
	(*env)->CallVoidMethod(env,thiz,methoID,child,params);
}

//得到子视图数量
int  ViewGroup_getChildCount(ViewGroup thiz)
{
	if(thiz==NULL)
	{
		LOGE("ViewGroup_getChildCount thiz==NULL");
		return 0;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getViewGroupClass(env);
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID(env,clazz,"getChildCount","()I");
	(*env)->DeleteLocalRef(env,clazz);
	return (*env)->CallIntMethod(env,thiz,methoID);
}

//得到子视图
View ViewGroup_getChildAt(ViewGroup thiz,int index)
{
	if(thiz==NULL)
	{
		LOGE("ViewGroup_getChildCount thiz==NULL");
		return 0;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getViewGroupClass(env);
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID(env,clazz,"getChildAt","(I)Landroid/view/View;");
	(*env)->DeleteLocalRef(env,clazz);
	jobject obj = (*env)->CallObjectMethod(env,thiz,methoID,index);
	View view = (*env)->NewGlobalRef(env,obj);
	(*env)->DeleteLocalRef(env,obj);
	return view;
}

//移除子视图
void ViewGroup_removeView(ViewGroup thiz,View child)
{
	if(thiz==NULL)
	{
		LOGE("ViewGroup_removeView thiz==NULL");
		return ;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getViewGroupClass(env);
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID(env,clazz,"removeView","(Landroid/view/View;)V");
	(*env)->DeleteLocalRef(env,clazz);
	(*env)->CallVoidMethod(env,thiz,methoID,child);
}

//移除子视图
void ViewGroup_removeViewAt(ViewGroup thiz,int index)
{
	if(thiz==NULL)
	{
		LOGE("ViewGroup_removeViewAt thiz==NULL");
		return ;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getViewGroupClass(env);
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID(env,clazz,"removeViewAt","(I)V");
	(*env)->DeleteLocalRef(env,clazz);
	(*env)->CallVoidMethod(env,thiz,methoID,index);
}

//移除全部子视图
void ViewGroup_removeAllViews(ViewGroup thiz)
{
	if(thiz==NULL)
	{
		LOGE("ViewGroup_removeAllViews thiz==NULL");
		return ;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getViewGroupClass(env);
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID(env,clazz,"removeAllViews","()V");
	(*env)->DeleteLocalRef(env,clazz);
	(*env)->CallVoidMethod(env,thiz,methoID);
}


//创建一个LayoutParams
LayoutParams newLayoutParams(int width,int height)
{
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getClass(env,"android/view/ViewGroup$LayoutParams");
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID( env,clazz,"<init>","(II)V" );
	jobject obj = (*env)->NewObject(env,clazz,methoID,width,height);
	LayoutParams params = (*env)->NewGlobalRef(env,obj);
	(*env)->DeleteLocalRef(env,obj);
	(*env)->DeleteLocalRef(env,clazz);
	return params;
}

//删除一个LayoutParams
void deleteLayoutParams(LayoutParams thiz)
{
	if(thiz==NULL)
	{
		LOGE("deleteLayoutParams thiz==NULL");
		return ;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	(*env)->DeleteGlobalRef(env,thiz);
}


//设置布局参数的宽度
void LayoutParams_setWidth(LayoutParams thiz,int width)
{
	if(thiz==NULL)
	{
		LOGE("LayoutParams_setWidth thiz==NULL");
		return ;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getClass(env,"android/view/ViewGroup$LayoutParams");
	static jfieldID fieldId= NULL;
	if(fieldId==NULL)
		fieldId = (*env)->GetFieldID(env,clazz,"width","I");
	(*env)->DeleteLocalRef(env,clazz);
	(*env)->SetIntField(env,thiz,fieldId,width);
}

//设置布局参数的高度
void LayoutParams_setHeight(LayoutParams thiz,int height)
{
	if(thiz==NULL)
	{
		LOGE("LayoutParams_setWidth thiz==NULL");
		return ;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getClass(env,"android/view/ViewGroup$LayoutParams");
	static jfieldID fieldId= NULL;
	if(fieldId==NULL)
		fieldId = (*env)->GetFieldID(env,clazz,"height","I");
	(*env)->DeleteLocalRef(env,clazz);
	(*env)->SetIntField(env,thiz,fieldId,height);
}

//得到布局参数的宽度
int  LayoutParams_getWidth(LayoutParams thiz)
{
	if(thiz==NULL)
	{
		LOGE("LayoutParams_setWidth thiz==NULL");
		return 0;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getClass(env,"android/view/ViewGroup$LayoutParams");
	static jfieldID fieldId= NULL;
	if(fieldId==NULL)
		fieldId = (*env)->GetFieldID(env,clazz,"width","I");
	(*env)->DeleteLocalRef(env,clazz);
	return (*env)->GetIntField(env,thiz,fieldId);
}

//得到布局参数的高度
int  LayoutParams_getHeight(LayoutParams thiz)
{
	if(thiz==NULL)
	{
		LOGE("LayoutParams_setWidth thiz==NULL");
		return 0;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getClass(env,"android/view/ViewGroup$LayoutParams");
	static jfieldID fieldId = NULL;
	if(fieldId==NULL)
		fieldId = (*env)->GetFieldID(env,clazz,"height","I");
	(*env)->DeleteLocalRef(env,clazz);
	return (*env)->GetIntField(env,thiz,fieldId);
}
